﻿//商品显示页js
var imgleft = 0;
var imgRight = 4;
var imgCount = 0;
$(document).ready(function () {
    $("#addFavorite").bind("click", AddFavorite);
    $("#addShopping").bind("click", AddShopping);
    $(".goodintro>li").bind("click", ChangeGoodsInfo);
    $(".centerBox img").bind("click", changeImage);
    $(".centerBox img").hover(changeImage, null);
    $("#imgNatural").jqueryzoom({
        xzoom: 350, //zooming div default width(default width value is 200)
        yzoom: 350, //zooming div default width(default height value is 200)
        offset: 10, //zooming div default offset(default offset value is 10)
        position: "right", //zooming div position(default position value is "right")
        preload: 1,
        lens: 0
    });

    $(".leftButton").bind("click", function () { var tag = -1; scollimg(tag) });
    $(".rightButton").bind("click", function () { var tag = 1; scollimg(tag) });

    imgCount = $(".centerBox li").length;
    $(".cutBox").bind("click", function () { countadd(-1); });
    $(".addBox").bind("click", function () { countadd(1); });

    $("input[name='pack']").bind("click", CalcPackPrice);
    $(".commendbuyButton").bind("click", function () { CommendBuy(); });

    $("#evalContent").load("/evaluate.aspx?id=" + goodid + "&page=1&r=" + Math.random());
    $("#consultcontent").load("/consult.aspx?id=" + goodid + "&page=1&r=" + Math.random());
});

function CommendBuy() {
    var goodslist = Array();
    goodslist.push(goodid);
    var commendlist = $("input[name='pack']:checked");
    for (var i = 0; i < commendlist.length; i++) {
        goodslist.push(commendlist.eq(i).attr("goodsid"))
    }
    var url = "/as/showcommon.aspx?Action=CommendBuy&r=" + Math.random();
    var data = "GoodsID=" + goodslist.join(",");
    $.ajax(
    {
        type: "GET",
        url: url,
        data: data,
        datatype: "JSON",
        success: function (ret) {
            if (ret) {
                alert("操作成功");
                window.location.href = "/home/orderinfo.aspx";
            }
            else {
                alert("系统提示：下订单失败，请与管理员联系");
            }
        }
    });

}

function CalcPackPrice() {
    var selectedcommend = $("input[name='pack']:checked");
    var price = 0;
    for (var i = 0; i < selectedcommend.length; i++) {
        price += parseInt($(selectedcommend[i]).attr("saleprice"));
    }
    price += parseInt($(".commendprice").attr("saleprice"));
    $(".commendprice,.dolorUnderline").html("￥" + price);
}

function countadd(tag) {
    if ((parseInt($(".numBox").val()) + tag) <= 0) {
        $(".numBox").val(1);
    }
    else {
        $(".numBox").val((parseInt($(".numBox").val()) + tag));
    }
}

function scollimg(tag) {

    var imglist = $(".centerBox li");
    if (tag > 0) {
        if (imgRight + tag < imgCount) {
            $(".centerBox li").hide();
            for (var i = (imgRight + tag - 4 > 0 ? imgRight + tag - 4 : 0); i <= imgRight + tag; i++) {
                $(imglist[i]).show();
            }
            imgRight = imgRight + tag;
            imgleft = imgleft + tag;
        }
    }
    else {
        if (imgleft + tag >= 0) {
            $(".centerBox li").hide();
            for (var i = imgleft + tag; i < imgCount && i < imgleft + tag + 5; i++) {
                $(imglist[i]).show();
            }
            imgRight = imgRight + tag;
            imgleft = imgleft + tag;
        }
    }

}

function changeImage() {

    $(".imgNatural").attr("src", $(this).attr("mtag")).attr("jqimg", $(this).attr("otag"));
    $("#imgNatural").jqueryzoom({
        xzoom: 350, //zooming div default width(default width value is 200)
        yzoom: 350, //zooming div default width(default height value is 200)
        offset: 10, //zooming div default offset(default offset value is 10)
        position: "right", //zooming div position(default position value is "right")
        preload: 1,
        lens: 0
    });

}

function ChangeGoodsInfo() {
    $(".detailedProduct").hide();
    $(".goodintro>li").removeClass("commodityIntroduction").addClass("normsLi");
    $(this).removeClass("normsLi").addClass("commodityIntroduction");
    $("#div" + $(this).attr("id")).show();
}

function AddFavorite() {

    var action = "AddFavorite";
    var success = function (ret) {
        eval("ret=" + ret);
        if (ret.Result == 1) {
            alert("该商品已经添加到关注列表,可以通过管理中的关注列表随时查看。");
        }
        else {

            if (!ret) {
                alert("您还没有登录，请登录后关注该商品");
                window.location.href = "/home/login.aspx";
            }
            else {
                alert(ret.Message);
            }
        }
    }
    if (userid == 0) {
        alert("您还没有登录，请登录后关注该商品");
        return;
    }
    var data = "GoodsID=" + goodid;
    GoodShowAjaxPost(action, data, success, "GET");
}

function AddShopping() {
    var action = "AddShopping";
    var success = function (ret) {
        eval("ret=" + ret);

        if (ret.Result == 1) {
            location.href = "/home/shoppingcarts.aspx?BackUrl=" + location.href; //购物车界面
            /*
            if (confirm("该商品已经添加到购物车。继续购物？")) {
                if (LoadingShoppingCart) {
                    LoadingShoppingCart();
                }
            }
            else {
                location.href = "/home/shoppingcarts.aspx"; //购物车界面
            }
            */
        }
        else {
            if (!ret) {
                alert("您还没有登录");
            }
            else {
                alert(ret.Message);
            }
        }
    }
    var data = "GoodsID=" + goodid;
    data += "&Quantity=" + $(".numBox").val();
    GoodShowAjaxPost(action, data, success, "GET");
}

function GoodShowAjaxPost(action, data, success1, posttype) {
    var url = "/as/showcommon.aspx?Action=" + action + "&r=" + Math.random();

    $.ajax(
    {
        type: posttype,
        url: url,
        data: data,
        success: success1
    });
}